package com.agritech.smartfarming.model;

import com.agritech.smartfarming.strategy.EnergyEfficientPathStrategy;

/**
 * 收割机类，继承自农机设备基类
 */
public class Harvester extends AgriculturalMachine {
    private double grainTankCapacity; // 粮仓容量 (kg)
    private double currentGrainAmount; // 当前粮食量 (kg)
    private double harvestingWidth; // 收割宽度 (m)
    
    public Harvester(String id, String name) {
        super(id, name, "收割机");
        this.grainTankCapacity = 8000.0;
        this.currentGrainAmount = 0.0;
        this.harvestingWidth = 6.0;
        // 默认使用能源效率路径规划策略
        this.pathStrategy = new EnergyEfficientPathStrategy();
    }
    
    @Override
    public void performTask() {
        if (status == MachineStatus.WORKING) {
            System.out.println(name + " 正在执行收割任务，收割宽度: " + harvestingWidth + "m");
            // 模拟收割过程中粮仓填充
            double harvestedAmount = 500.0;
            addGrain(harvestedAmount);
        } else {
            System.out.println(name + " 当前不在工作状态，无法执行任务");
        }
    }
    
    @Override
    public void update(EnvironmentData environmentData) {
        System.out.println(name + " 接收到环境数据更新");
        
        // 根据环境数据调整收割参数
        if (environmentData.getCropHeight() < 50) {
            System.out.println("⚠️ " + name + " 检测到作物高度较低 (" + environmentData.getCropHeight() + 
                "cm)，调低割台高度");
        }
        
        if (environmentData.getSoilMoisture() > 85) {
            System.out.println("⚠️ " + name + " 检测到土壤湿度过高 (" + environmentData.getSoilMoisture() + 
                "%)，注意防止陷入，降低行驶速度");
        }
        
        if (environmentData.getWindSpeed() > 15) {
            System.out.println("⚠️ " + name + " 检测到风速过大 (" + environmentData.getWindSpeed() + 
                "m/s)，可能影响收割质量，建议暂停作业");
            if (status == MachineStatus.WORKING) {
                status = MachineStatus.IDLE;
                System.out.println(name + " 自动暂停收割作业");
            }
        }
        
        // 更新位置信息
        if (environmentData.getPosition() != null) {
            double distance = calculateDistance(this.position, environmentData.getPosition());
            if (distance < 100) {  // 如果环境数据采集点在农机100米范围内
                System.out.println(name + " 更新当前区域环境参数，调整收割高度和速度");
            }
        }
    }
    
    /**
     * 计算两点之间的距离
     */
    private double calculateDistance(Position p1, Position p2) {
        double latDiff = p2.getLatitude() - p1.getLatitude();
        double longDiff = p2.getLongitude() - p1.getLongitude();
        return Math.sqrt(latDiff * latDiff + longDiff * longDiff) * 111000;
    }
    
    /**
     * 添加收获的粮食到粮仓
     */
    public void addGrain(double amount) {
        if (currentGrainAmount + amount <= grainTankCapacity) {
            currentGrainAmount += amount;
            System.out.println(name + " 收获粮食 " + amount + "kg，当前粮仓存量: " + 
                currentGrainAmount + "kg，容量: " + grainTankCapacity + "kg");
        } else {
            System.out.println("⚠️ " + name + " 粮仓已满，需要卸粮");
            status = MachineStatus.IDLE;
        }
    }
    
    /**
     * 卸粮
     */
    public void unloadGrain() {
        System.out.println(name + " 正在卸粮，卸出 " + currentGrainAmount + "kg 粮食");
        currentGrainAmount = 0.0;
        System.out.println(name + " 粮仓已清空，可继续作业");
    }
    
    /**
     * 获取收割机特有信息
     */
    public String getHarvesterInfo() {
        return String.format("粮仓容量: %.1f kg, 当前粮食量: %.1f kg (%.1f%%), 收割宽度: %.1f m", 
            grainTankCapacity, currentGrainAmount, (currentGrainAmount / grainTankCapacity) * 100, harvestingWidth);
    }
} 